package com.atguigu.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.base.BaseController;
import com.atguigu.entity.Role;
import com.atguigu.service.PermissionService;
import com.atguigu.service.RoleService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/role")
public class RoleController extends BaseController {
    @Reference
    private RoleService roleService;
    @Reference
    private PermissionService permissionService;


/*    @RequestMapping
    public String findAll(Model model){
        List<Role> roleList = roleService.findAll();
        model.addAttribute("list",roleList);
        return "role/index";
    }*/
    @PreAuthorize("hasAuthority('role.show')")
    @RequestMapping
    public String findPage(HttpServletRequest request, Model model){
        //从前端拿到查询条件以及分页参数
        Map<String, Object> filters = getFilters(request);
        //调用业务层,获取分页相关的数据和参数
        PageInfo<Role> pageInfo = roleService.findPage(filters);
        model.addAttribute("page",pageInfo);
        model.addAttribute("filters",filters);
        //跳转页面
        return "role/index";
    }

    @PreAuthorize("hasAuthority('role.create')")
    @RequestMapping("/create")
    public String create(){

        return "role/create";
    }
    @PreAuthorize("hasAuthority('role.create')")
    @PostMapping("/save")
    public String save(Role role){
        this.roleService.insert(role);
        return "common/successPage";
    }

    @PreAuthorize("hasAuthority('role.edit')")
    @RequestMapping("/edit/{id}")
    public String edit(@PathVariable Long id,Model model){
        Role role = this.roleService.getById(id);
        model.addAttribute("role",role);
        return "role/edit";
    }
    @PreAuthorize("hasAuthority('role.edit')")
    @PostMapping("/update")
    public String update(Role role){
        this.roleService.update(role);
        return "common/successPage";
    }
    @PreAuthorize("hasAuthority('role.delete')")
    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable Long id){
        this.roleService.delete(id);
        return "redirect:/role";
    }

    //opt.openWin("/role/assignShow/"+id,'修改',580,430);  前端传入角色ID.
    @PreAuthorize("hasAuthority('role.assgin')")
    @RequestMapping("/assignShow/{id}")
    public String assignShow(@PathVariable("id") Long roleId,Model model){
        //根据角色id查询到所有权限以及角色具有的权限数据,转发到修改的页面去
        List<Map<String, Object>> zNodes = permissionService.findPermissionsByRoleId(roleId);
        model.addAttribute("zNodes",zNodes);
        model.addAttribute("roleId",roleId);
        return "role/assignShow";
    }
    @PreAuthorize("hasAuthority('role.assgin')")
    //action="@{/role/assignPermission}
    @RequestMapping("/assignPermission")
    public String assignPermission(Long roleId,Long[] permissionIds){
        permissionService.saveRolePermissionRealtionShip(roleId,permissionIds);
        return "common/successPage";
    }
}
